package com.lyd.mybatis.demo.app.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.lyd.mybatis.demo.app.convert.UserConvert;
import com.lyd.mybatis.demo.app.model.dto.UserDTO;
import com.lyd.mybatis.demo.app.model.dto.UserQueryDTO;
import com.lyd.mybatis.demo.app.model.pojo.User;
import com.lyd.mybatis.demo.app.model.vo.UserVO;
import com.lyd.mybatis.demo.mapper.UserMapper;
import com.lyd.sweet.mybatis.service.AbstractBasicService;
import com.lyd.sweet.mybatis.utils.PageUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends AbstractBasicService<User, UserQueryDTO, UserDTO, UserVO>  {

    private final UserConvert userConvert;
    private final UserMapper userMapper;

    public UserService(UserConvert userConvert, UserMapper userMapper) {
        super(userConvert, userMapper);
        this.userConvert = userConvert;
        this.userMapper = userMapper;
    }

    public Boolean disabled(Long id){
        UserDTO dto = new UserDTO();
        dto.setUserId(id);
        dto.setIsEnabled(false);
        return this.update(dto);
    }

    public Boolean enabled(Long id) {
        UserDTO dto = new UserDTO();
        dto.setUserId(id);
        dto.setIsEnabled(true);
        return this.update(dto);
    }

    @Override
    public PageInfo<UserVO> pages(UserQueryDTO queryDTO) {
        Page page = PageUtil.buildQueryPage(queryDTO);
        //重写查询列表SQL,扩展多表关联查询
        List<User> users = this.userMapper.query(queryDTO);
        List<UserVO> list = this.userConvert.toVoList(users);
        return PageUtil.buildResult(page, list);
    }

    @Override
    public List<UserVO> list(UserQueryDTO queryDTO) {
        //重写查询列表SQL,扩展多表关联查询
        List<User> users = this.userMapper.query(queryDTO);
        List<UserVO> list = this.userConvert.toVoList(users);
        return list;
    }
}
